<?php

namespace App\Http\Controllers\Api;

use App\Models\FarmerFeedback;
use App\Models\FeedbackAppeal;
use App\Models\FeedbackOpinion;
use App\Models\IntegralRule;
use App\Models\PaperCommit;
use App\Models\PaperFeedback;
use App\Services\SensitiveWords;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class FeedbackController extends Controller
{
    public function __construct()
    {

    }

    // 民情反馈
    public function farmerForm(Request $request){


        $message = [
            'type.required' => '请选择问题类型',
            'content.required' => '请输入内容',
            'region_id.required' => '请选择地区',
        ];

        $validator = Validator::make($request ->all(),[
            'type' => 'required',
            'content' => 'required',
            'region_id' => 'required',
        ],$message);

        if($validator -> fails()){
            return jsonError($validator -> messages() -> first());
        }

        $bad_word = SensitiveWords::getBadWord($request -> input('content'));
        if (!empty($bad_word)) {
            return jsonError('请勿使用敏感词：'.current($bad_word));
        }

        $farmerFeedback = FarmerFeedback::store($request ->all());

        if($farmerFeedback){

            return jsonSuccess(2007);
        }else{
            return jsonError(4018);
        }
    }

    // 个人中心--意见反馈
    public function opinions(Request $request){

        $message = [
            'content.required' => '请输入内容',
            'contact.required' => '请填写联系方式',
        ];

        $validator = Validator::make($request ->all(),[
            'content' => 'required',
            'contact' => 'required',
        ],$message);

        if($validator -> fails()){
            return jsonError($validator -> messages() -> first());
        }

        $bad_word = SensitiveWords::getBadWord($request -> input('content'));
        if (!empty($bad_word)) {
            return jsonError('请勿使用敏感词：'.current($bad_word));
        }


        $opinions = FeedbackOpinion::store($request -> all());
        if($opinions){

            return jsonSuccess(2007);
        }else{
            return jsonError(4018);
        }
    }


    // 调查问卷提交
    public function paperCommit(Request $request){

        $params  = $request -> only('paper_librarie_id','answer','region_id','real_name','mobile');

        $validator = Validator::make($request -> all(),[
            'real_name' => 'required',
            'mobile' => ['required','regex:/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/'],
        ],['real_name.required' => '请填写姓名','mobile.required' => '请填写手机号','mobile.regex' => '请填写正确的手机号']);

        if($validator -> fails()){
            return jsonError($validator -> messages() -> first());
        }

        // 检测某用户是否已答过此题
        $user_id = auth() -> id();
        if(PaperCommit::where('paper_librarie_id',$params['paper_librarie_id']) -> where('user_id',$user_id) -> first()){
            return jsonError(4037);
        }

        // 检验答题是否答完，题数是否够
        $res = PaperFeedback::paperCheck($params['paper_librarie_id'], $params['answer']);
        if(!$res){
            return jsonError(4029);
        }


        $paperCommit = PaperCommit::store($params);
        if($paperCommit){

            // 问卷调查，送积分
            $integral = IntegralRule::rule(7,$user_id,$params['paper_librarie_id']);
            return jsonSuccess(($integral ?: []),'',200,2007);
        }else{
            return jsonError(4018);
        }
    }

    // 获取某一张卷
    public function getPaper(Request $request)
    {
        $paper_librarie_id = $request -> input('paper_librarie_id');
        $region_id         = $request -> input('region_id');
        $user_id           = auth() -> id();
        $answered          = false;

        // 检测某用户是否已答过此题
        if(PaperCommit::where('paper_librarie_id',$paper_librarie_id) -> where('user_id',$user_id) -> first()){
             $answered = true;
        }

        $getPaper = PaperFeedback::getPaper($paper_librarie_id,$region_id);
        $getPaper -> answered = $answered;

        return jsonSuccess($getPaper);
    }

    // 诉求建议表
    public function appealForm(Request $request){

        $message = [
            'type.required' => '请选择一下诉求类型',
            'content.required' => '请填写诉求内容',
            'contact.required' => '请填写联系方式',
        ];
        $validator = Validator::make($request -> all(),[
            'type'    => 'required',
            'content' => 'required',
            'contact' => 'required'
        ],$message);

        if($validator -> fails()){
            return jsonError($validator -> messages() -> first());
        }

        $bad_word = SensitiveWords::getBadWord($request -> input('content'));
        if (!empty($bad_word)) {
            return jsonError('请勿使用敏感词：'.current($bad_word));
        }

        $contact = $request -> input('contact');
        if(is_numeric($contact)){
            $reg = '/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/';
            if(!preg_match($reg,$contact)){
                return jsonError('请输入正确的手机号');
            }
        }else{
            $reg = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/';
            if(!preg_match($reg,$contact)){
                return jsonError('请输入正确的邮箱');
            }
        }



        $appealForm = FeedbackAppeal::store($request -> all());
        if($appealForm){
            $user_id = auth() -> id();
            // 诉求建议 送积分
            $integralRule = IntegralRule::rule(8, $user_id, $appealForm -> id);
            return jsonSuccess(($integralRule ?: []),'',200,2007);
        }else{
            return jsonError(4018);
        }
    }
}
